/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.ulohy.uloha2.entities;

import java.math.BigDecimal;
import java.util.Date;

/**
 *
 * @author stefan
 */
public class Ownership {
    
    private Long id;
    private Owner owner;
    private RealEstate realEstate;
    private Date dateCreated;
    private Date dateEnded;
    private BigDecimal investment;
    
    public Ownership(Owner owner, RealEstate realEstate, BigDecimal investment) {
        if(null == owner || null == realEstate) {
            throw new IllegalArgumentException("owner nor realEstate cannot be null. owner=" + owner + ", realEstate=" + realEstate);
        } else if(investment.compareTo(new BigDecimal(0)) != 1) {
            throw new IllegalArgumentException("percentage must be between 1 and 100. percentage=" + investment);
        }
        this.owner = owner;
        this.realEstate = realEstate;
        this.investment = investment;
        this.dateCreated = new Date();
    }

    public void setId(Long id) {
        if(null == id) {
            throw new IllegalArgumentException("id cannot be null");
        }
        this.id = id;
    }
    
    public Long getId() {
        return this.id;
    }
    
    public Owner getOwner() {
        Owner owner = new Owner(this.owner.getSsn(), this.owner.getFirstName(), this.owner.getLastName());
        owner.setId(this.owner.getId());
        owner.setAddress(this.owner.getAddress());
        owner.setTelephone(this.owner.getTelephone());
        return owner;
    }

    public void setOwner(Owner owner) {
        if(null == owner) {
            throw new IllegalArgumentException("owner cannot be null");
        }
        
        this.owner = new Owner(owner.getSsn(), owner.getFirstName(), owner.getLastName());
        this.owner.setId(owner.getId());
        this.owner.setAddress(owner.getAddress());
        this.owner.setTelephone(owner.getTelephone());
    }

    public RealEstate getRealEstate() {
        RealEstate realEstate = new RealEstate(this.realEstate.getCatastralId(), this.realEstate.getType());
        realEstate.setId(this.realEstate.getId());
        return realEstate;
    }

    public void setRealEstate(RealEstate realEstate) {
        if(null == realEstate) {
            throw new IllegalArgumentException("realEstate cannot be null");
        }
        this.realEstate = new RealEstate(realEstate.getCatastralId(), realEstate.getType());
        this.realEstate.setId(realEstate.getId());
    }

    public Date getDateEnded() {
        if (null == this.dateEnded) return null;
        else return new Date(dateEnded.getTime());
    }

    public void setDateEnded(Date dateEnded) {
        if(null == dateEnded) {
            throw new IllegalArgumentException("dateEnded cannot be null");
        } else if(dateEnded.before(dateCreated)) {
            throw new IllegalArgumentException("dateEnded cannot be before dateCreated");
        }
        this.dateEnded = new Date(dateEnded.getTime());
    }

    public BigDecimal getInvestment() {
        return investment;
    }

    public void setInvestment(BigDecimal investment) {
        if(investment.compareTo(new BigDecimal(0)) != 1) {
            throw new IllegalArgumentException("investment must be greater than 0. investment=" + investment);
        }
        this.investment = investment;
    }

    public Date getDateCreated() {
        return new Date(dateCreated.getTime());
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Ownership other = (Ownership) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }


}
